/***************************************************************
 * 系统名称           : 生鲜MIS系统
 * 处理内容           : 自定义CheckedComboBoxEdit
 * @author            : 
 * @Modify            : geping@2019-01-21
 * @description       : 
 * @version           : v1.0
 * @Copyright         : 宁波康铭泰克信息科技有限公司
 ***************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Comteck.Utils;
using DevExpress.Accessibility;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;

namespace Comteck.Winforms.Controls {
  [ToolboxItem(true)]
  public partial class MyCheckedComboBoxEdit : CheckedComboBoxEdit {
    ToolTip toolTip = new ToolTip();

    static MyCheckedComboBoxEdit() {
      RepositoryItemMyCheckedComboBoxEdit.Register();
    }

    /// <summary>
    /// 
    /// </summary>
    public MyCheckedComboBoxEdit() : base() {
      Initialize();
    }

    /// <summary>
    /// 初始化界面
    /// </summary>
    private void Initialize() {
      // 全选时显示内容替换成“全部”
      this.CustomDisplayText += (sender, e) => {
        var list = this.Properties.Items.GetCheckedValues();

        if (list.Count == 0) { return;}

        if (list.Count == this.Properties.Items.Count) { e.DisplayText = "全部"; }
      };
      // 光标停留在控件上时显示提示
      this.MouseHover += (sender, e) => {
        this.toolTip.ShowAlways = true;
        this.toolTip.SetToolTip(this, this.Text);
      };
    }

    /// <summary>
    /// 返回CheckedComboBoxEdit中的选中值集合.  add by geping@2018-09-19
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public IList<T> GetCheckedValues<T>() {
      IList<T> list = Array.ConvertAll<object, T>(
        this.Properties.Items.GetCheckedValues().ToArray(),
        new Converter<object, T>(x => x.To<T>()));

      return list;
    }

    /// <summary>
    /// 设置多个默认选中项 add by geping@2019-04-02
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="values"></param>
    public void SetEditValue<T>(IList<T> values) {
      foreach (var value in values) {
        this.Properties.Items.Add(value, true);
      }
      this.RefreshEditValue();
    }

    /// <summary>
    /// 设置选中项  add by geping@2019-08-07
    /// </summary>
    /// <param name="values"></param>
    public void SetCheckedValue(List<object> values) {
      var list = this.Properties.Items.Select(x => (item : x, value : x.Value)).ToList();

      foreach (var item in list) {
        if (values.Exists(x => object.Equals(x, item.value))) {
          item.item.CheckState = CheckState.Checked;
        } else {
          item.item.CheckState = CheckState.Unchecked;
        }
      }

      this.RefreshEditValue();
    }
    
    #region fields

    /// <summary>
    /// 
    /// </summary>
    public override string EditorTypeName => RepositoryItemMyCheckedComboBoxEdit.EditorName;

    /// <summary>
    /// 
    /// </summary>
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public new RepositoryItemMyCheckedComboBoxEdit Properties => base.Properties as RepositoryItemMyCheckedComboBoxEdit;

    #endregion
  }

  /// <summary>
  /// 
  /// </summary>
  [DXCategory("Properties")]
  [UserRepositoryItem("Register")]
  [ToolboxItem(true)]
  public class RepositoryItemMyCheckedComboBoxEdit : RepositoryItemCheckedComboBoxEdit {

    /// <summary>
    /// 
    /// </summary>
    static RepositoryItemMyCheckedComboBoxEdit() {
      Register();
    }

    /// <summary>
    /// 
    /// </summary>
    public RepositoryItemMyCheckedComboBoxEdit() {
    }

    /// <summary>
    /// 
    /// </summary>
    internal const string EditorName = nameof(MyCheckedComboBoxEdit);

    /// <summary>
    /// 
    /// </summary>
    public override string EditorTypeName => EditorName;

    //
    // 摘要:
    //     Gets or sets whether value scrolling via the Mouse Wheel is supported by the
    //     editor.
    [Browsable(false)]
    [DefaultValue(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [DXCategory("Behavior")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public override bool AllowMouseWheel { get; set; } = false;

    /// <summary>
    /// 注册新编辑器
    /// </summary>
    public static void Register() {
      EditorRegistrationInfo.Default.Editors.Add(
        new EditorClassInfo(
          EditorName, // Specifies the editor's name.
          typeof(MyCheckedComboBoxEdit), // Specifies the type of the editor class.
          typeof(RepositoryItemMyCheckedComboBoxEdit),  // Specifies the type of the custom repository item class.
          typeof(PopupContainerEditViewInfo),  // Specifies the type of the editor's ViewInfo class.
          new ButtonEditPainter(),  // An instance of the editor's Painter class.
          true,                     // will be visible at design time within container controls
                                    // (for instance, when creating an in-place editor
                                    // within the XtraGrid or XtraBars).
          null,                     // Specifies the image that will be displayed along
                                    // with the editor's name within container controls.
                                    // Specifies the type of the object 
          typeof(PopupEditAccessible)));   // that provides accessibility information.
    }
  }
}
